<script type="text/javascript">
var isNew;
var choiseamount = 3;
var currectupdaterdiv = "";
var currectupdatermode = false;
var currectupdater = "";
$(document).ready(function() {
    reloadListener();
    checkImageUpdaterScript();
    checkBigFileUpdaterScript();
    data = JSON.parse($(".s_temple_setting.question_editor").html());
    isNew = data.isNew;
    chengeMode(0);
    $("#s_q_f_subject").html(data.subject.displayname);
    $("#choiseinputtemple").hide();
    $("#choiseanswertemple").hide();
    currectupdaterdiv = "";
    currectupdatermode = false;
    currectupdater = "";
    $("#s_q_e_q_a").hide();
    $("#s_q_e_q_e").hide();
    $(".s_question_appends").hide();
    $(".s_explain_appends").hide();
    $("#s_append_temple").hide();
    if(data.showSuccMessage){
    	 showMessage("s_q_e_message", 0, "您的试题已经创建成功");
    }
    $("#s_q_f_type").attr("value", isNew ? "true": "false");
    if (!isNew) {
        $("#s_q_p_editor").html("编辑试题");
        $("#s_q_f_qid").attr("value", data.qid);
        $("#s_q_f_sid").attr("value", data.subject.sid);
        //老操作
        var ques = data.data;
        view_level = ques.view_level;
        
        $("#view_level_" + view_level).click();
        $("#answer_view_level_"+ques.answer_view_level).click();
        $("#explain_view_level_"+ques.explain_view_level).click();
        $("#type").attr("value", ques.label);
        $("#s_description").html(ques.description);
        question = ques.question;
        $("#question_type_" + question.type).click();
      
        
        if (question.type == 0) {
            $.each(question.choises,
            function(k, v) {
                if (parseInt(k) > choiseamount) {
                    addChoise();
                } 
                $("#question_choise_" + k).attr("value", v);
            });
            $("#s_choose_answer_" + data.data.answer).click();
        }
        if (question.type == 1) $("#s_judze_answer_" + data.data.answer).click();
        if (question.type == 2) $("#question_answer").html(data.data.answer);
        $.each(question.appends.image,
        function(k, v) {
            $(".s_question_appends").show();
     
            s_addEditAppend(v, k, "_image", false, false)
        });
        $.each(question.appends.video,
        function(k, v) {
        	 $(".s_explain_appends").show();
            s_addEditAppend(v, k, "_video", true, false)
        });
        $("#question_header").html(new Base64().decode(question.header));
        $("#qexplain").html(new Base64().decode(data.data.qexplain.explain));
        $.each(data.data.qexplain.appends.image,
        function(k, v) { 
            $(".s_explain_appends").show();
            s_addEditAppend(v, k, "_image", false, true)
        });
        $.each(data.data.qexplain.appends.video,
        function(k, v) { 
            $(".s_explain_appends").show();
            s_addEditAppend(v, k, "_video", true, true)
        });
    } else {
        $("#s_q_p_editor").html("发布试题");
        console.log("new");
    }
    bindAjaxForm($("#s_q_f_form"), s_q_e_onSuccess, s_q_e_onError, "di");

});

function s_q_e_onSuccess(data) {
    console.log(data);
    s_q_e_operatedone(data.code, data.message, data.data);
}

function s_q_e_onError(data) {
    console.log(data);
    s_q_e_operatedone( - 101, "服务器返回了未知的数据，请上报管理员", data.responseText);
}

function s_q_e_operatedone(id, message, data) {
    if (id == -101) {
        showMessage("s_q_e_message", -101, message + "<br><br><pre>" + data + "</pre>");
        return;
    }
    if (id != 0 && id != 3) {
        showMessage("s_q_e_message", id, message);
        return;
    }
    if (id == 0) {
        showMessage("s_q_e_message", id, message);
    }
    if(data.op=="reset"){
    	$("#s_ajax_container").load("index.php?mod=question&childmod=personal&ajax=true&op=createQuestion&success=true")
    }
}

function onImageUpdaterScriptLoad() {

}

function onBigFileUpdaterScriptLoad() {
    s_b_f_setAccept("video/*");
}

function image_update_callback(state, data) {
    if (state) {
        s_addappend(data.data);
    }
}

function s_b_f_done_callback(data) {
    console.log(data);
    s_addappend(data);
}

function chengeMode(mode) {
    if (mode == 0) {
        $(".s_choise").show();
        $(".s_main").hide();
        $(".s_judze").hide();
    }
    if (mode == 1) {
        $(".s_choise").hide();
        $(".s_main").hide();
        $(".s_judze").show();
    }
    if (mode == 2) {
        $(".s_choise").hide();
        $(".s_main").show();
        $(".s_judze").hide();
    }
}

function addChoise() {
    choiseamount++;
    if (choiseamount >= 5) {
        $("#addChoise").hide();
        if (choiseamount > 5) return;
    }
    var $tmp = $("#choiseinputtemple").clone();
    $tmp.show();
    $tmp.attr("id", "s_choose_answer_" + choiseamount);
    $tmp.find("label").html("选择题选项" + choiseamount);
    $tmp.find("input").attr("placeholder", "选择题选项" + choiseamount).attr("name", "choise" + choiseamount).attr("id", "question_choise_" + choiseamount);
    $("#addChoise").before($tmp);
    var $tmpa = $("#choiseanswertemple").clone();
    $tmpa.show();
    $tmpa.find("label").html("选择题选项" + choiseamount);
    $tmpa.find("input").attr("value", choiseamount);
    $(".s_choise_answer").append($tmpa);
    $(".q_choise_amount").attr("value", choiseamount);
    $('.ui.checkbox').checkbox();
}

function chengeupdater(mode, div, root) {
    if (currectupdaterdiv != "") {
        if (currectupdatermode) {
            s_b_f_uninstall();
        } else {
            s_i_uninstall();
        }
        $("#" + currectupdater).hide();
    }
    currectupdaterdiv = div;
    currectupdatermode = mode;
    currectupdater = root;
    $("#" + root).show();
    if (mode) {
        s_b_f_load(div);
    } else {
        s_i_load(div);
    }
}
function s_addEditAppend(url, rid, appendtype, isVedio, isExplore) {
    var $tmp = $("#s_append_temple").clone();
    $tmp.show();
    if (isVedio) {
        $tmp.find(".s_type").attr("type", "media").attr("webpath", url);
        $tmp.find("img").attr("src", "assets/images/file.png");
        $tmp.find(".content").html("视频文件：");
        appendtype = "_video";
    } else {
        $tmp.find(".s_type").html("image").attr("webpath", url);
        $tmp.find("img").attr("src", url);
        $tmp.find(".content").html("图片文件");
    }
    $tmp.find(".s_rid").attr("rid", rid);
    $tmp.attr("id", "s_append_" + rid);
    if (isExplore) {
        $tmp.find(".s_selector").attr("name", "explain_appends" + appendtype + "[]").attr("value", rid);
        $(".s_explore_appends").show();
        $("#explore_appends").append($tmp);
    } else {
        $tmp.find(".s_selector").attr("name", "question_appends" + appendtype + "[]").attr("value", rid);
        $(".s_question_appends").show();
        $("#question_appends").append($tmp);
    }
}
function s_addappend(data) {
    var $tmp = $("#s_append_temple").clone();
    $tmp.show();
    console.log(data);
    var appendtype = "_image";
    if (currectupdatermode) {
        $tmp.find(".s_type").attr("type", "media").attr("webpath", data.webPath);
        $tmp.find("img").attr("src", "assets/images/file.png");
        $tmp.find(".content").html("视频文件：" + data.orgname);
        appendtype = "_video";
    } else {
        $tmp.find(".s_type").html("image").attr("webpath", data.webPath);
        $tmp.find("img").attr("src", data.webPath);
        $tmp.find(".content").html("图片文件");
    }
    $tmp.find(".s_rid").attr("rid", data.rid);
    $tmp.attr("id", "s_append_" + data.rid);
    if (currectupdater == "s_q_e_q_e") {
        $tmp.find(".s_selector").attr("name", "explain_appends" + appendtype + "[]").attr("value", data.rid);
        $(".s_explore_appends").show();
        $("#explain_appends").append($tmp);
    } else {
        $tmp.find(".s_selector").attr("name", "question_appends" + appendtype + "[]").attr("value", data.rid);
        $(".s_question_appends").show();
        $("#question_appends").append($tmp);
    }
}
function s_preview_append(e) {
    $("#_preview").remove();
    if (e.hasClass("active")) {
        e.removeClass("active");
        e.html("预览");
        return;
    } else {
        e.addClass("active");
        e.html("取消预览");
    }
    var $div = $("<div id='_preview'>");
    if (e.parent().parent().find(".s_type").attr("type") == "media") {
        var dif = $("<div class='append_video'>");
        dif.append($("<video controls autoplay loop muted preload='auto'>").attr("src", e.parent().parent().find(".s_type").attr("webpath")));
        $div.append(dif);
    } else {
        console.log(e.parent().parent().find(".s_type").attr("webpath"));
        $div.append($("<img class='ui medium image zoomify'>").attr("src", e.parent().parent().find(".s_type").attr("webpath")).zoomify());
    }
    e.parent().parent().after($div);
}
function s_remove_append(rid) {
    $("#s_append_" + rid).remove();
}
</script>
<div class="s_temple_setting question_editor"><?php print_json_setting("question_editor");?></div>
<div class=" ui message hidden" id="s_q_e_message"></div>
<form class="ui form ajaxform" id="s_q_f_form" url="index.php?mod=question&childmod=personal&ajax=ture&op=updateedit">
	<input id="s_q_f_type" type="hidden" name="isCreate">
	<input id="s_q_f_qid" type="hidden" name="qid">
	<input id="s_q_f_sid" type="hidden" name="sid">
	<div class="field">
		<label>
			科目：
			<span id="s_q_f_subject"></span>
		</label>
	</div>
	<h2 class="ui dividing header">题干信息</h2>
	<div class="field">
		<label>题目简述</label>
		<textarea id="s_description" rows="2" name="description" form="s_q_f_form"></textarea>
	</div>
	<div class="field">
		<label>题目分类，诸如单元名称章节名称知识点名称等，用于检索，推荐使用逗号分隔</label>
		<input type="text" name="type" id="type">
	</div>
	<div class="grouped fields">
		<label for="fruit">阅读权限</label>
		<div class="field">
			<div id="view_level_0" class="ui radio checkbox">
				<input type="radio" name="view_level" value="0" checked="" tabindex="0" class="hidden">
				<label>仅自己可见（适用于考试出题等）</label>
			</div>
		</div>
		<div id="view_level_1" class="field">
			<div class="ui radio checkbox">
				<input type="radio" name="view_level" value="1" tabindex="0" class="hidden"">
				<label>教师团队可见（适用于团伙合作出题）</label>
			</div>
		</div>
		<div id="view_level_2" class="field">
			<div class="ui radio checkbox">
				<input type="radio" name="view_level" value="2" tabindex="0" class="hidden">
				<label>公开（在公开列表里，可以被学生推送收藏等）</label>
			</div>
		</div>
	</div>
	<div class="grouped fields">
		<label for="question_type">题目类型</label>
		<div class="field">
			<div id="question_type_0" class="ui radio checkbox" onClick="chengeMode(0)">
				<input type="radio" name="q_type" value="0" checked="" tabindex="0" class="hidden">
				<label>选择</label>
			</div>
		</div>
		<div class="field">
			<div id="question_type_1" class="ui radio checkbox" onClick="chengeMode(1)">
				<input type="radio" name="q_type" value="1" tabindex="0" class="hidden"">
				<label>判断（请将题干直接填写在题目叙述里）</label>
			</div>
		</div>
		<div class="field">
			<div id="question_type_2" class="ui radio checkbox" onClick="chengeMode(2)">
				<input type="radio" name="q_type" value="2" tabindex="0" class="hidden">
				<label>主观（请将题干直接填写在题目叙述里）</label>
			</div>
		</div>
	</div>
	<div class="field">
		<label>题干信息</label>
		<textarea id="question_header" rows="2" name="q_header" form="s_q_f_form"></textarea>
	</div>
	<input type="hidden" name="choise_amount" value="3" class="q_choise_amount">
	<div class="s_choise s_choise_question">
		<div class="field" id="choiseinputtemple">
			<label></label>
			<input id="question_choise_n" placeholder="选择题选项n" type="text" name="choise3">
		</div>
		<div class="field">
			<label>第1个选项</label>
			<input id="question_choise_1" placeholder="选择题选项1" type="text" name="choise1">
		</div>
		<div class="field">
			<label>第2个选项</label>
			<input id="question_choise_2" placeholder="选择题选项2" type="text" name="choise2">
		</div>
		<div class="field">
			<label>第3个选项</label>
			<input id="question_choise_3" placeholder="选择题选项3" type="text" name="choise3">
		</div>
		<div class="field" id="addChoise">
			<label>增加一个选项</label>
			<a onClick="addChoise()">+ 增加</a>
		</div>
	</div>
	<div class="s_question_appends s_appends">
		<h5 class="ui dividing header">附件列表</h5>
		<div class="ui divided items" id="question_appends"></div>
	</div>
	<div class="ui compact menu s_append_menu">
		<a class="item s_activity_bar" onClick="chengeupdater(false,'s_q_e_q_a_div','s_q_e_q_a');">上传图片</a>
		<a class="item s_activity_bar" onClick="chengeupdater(true,'s_q_e_q_a_div','s_q_e_q_a');">上传视频</a>
	</div>
	<div class="ui attached segment" id="s_q_e_q_a" style="padding-top: 1cm; padding-bottom: 1cm;">
		<div id="s_q_e_q_a_div"></div>
	</div>
	<h2 class="ui dividing header">答案</h2>
	<div class="inline fields">
		<label for="fruit">答案是否对学生和其他人可见</label>
		<div class="field">
			<div class="ui radio checkbox">
				<input type="radio" id="answer_view_level_1" name="answer_view_level" value="1" checked="checked" tabindex="0" class="hidden">
				<label>可见</label>
			</div>
		</div>
		<div class="field">
			<div class="ui radio checkbox">
				<input type="radio" id="answer_view_level_0" name="answer_view_level" value="0" tabindex="0" class="hidden">
				<label>不可见</label>
			</div>
		</div>
	</div>
	<div class="field s_main">
		<label>主观题答案</label>
		<textarea id="question_answer" rows="2" name="question_answer_text" form="s_q_f_form"></textarea>
	</div>
	<div class="inline fields s_judze">
		<label for="fruit">判断题答案</label>
		<div class="field">
			<div class="ui radio checkbox">
				<input type="radio" id="s_judze_answer_1" name="question_answer_judze" value="1" checked="checked" tabindex="0" class="hidden">
				<label>正确</label>
			</div>
		</div>
		<div class="field">
			<div class="ui radio checkbox">
				<input type="radio" id="s_judze_answer_0" name="question_answer_judze" value="0" tabindex="0" class="hidden">
				<label>错误</label>
			</div>
		</div>
	</div>
	<div class="inline fields s_choise s_choise_answer">
		<label for="fruit">选择题答案</label>
		<div class="field" id="choiseanswertemple">
			<div class="ui radio checkbox">
				<input type="radio" name="question_answer_choose" value="3" tabindex="0" class="hidden">
				<label>第二个选项</label>
			</div>
		</div>
		<div class="field">
			<div id="s_choose_answer_1" class="ui radio checkbox">
				<input type="radio" name="question_answer_choose" value="1" checked="checked" tabindex="0" class="hidden">
				<label>第一个选项</label>
			</div>
		</div>
		<div class="field">
			<div id="s_choose_answer_2" class="ui radio checkbox">
				<input type="radio" name="question_answer_choose" value="2" tabindex="0" class="hidden">
				<label>第二个选项</label>
			</div>
		</div>
		<div class="field">
			<div id="s_choose_answer_3" class="ui radio checkbox">
				<input type="radio" name="question_answer_choose" value="3" tabindex="0" class="hidden">
				<label>第三个选项</label>
			</div>
		</div>
	</div>
	<h2 class="ui dividing header">解析</h2>
	<div class="inline fields">
		<label for="fruit">解析是否对学生和其他人可见</label>
		<div class="field">
			<div class="ui radio checkbox">
				<input type="radio" id="explain_view_level_1" name="explain_view_level" value="1" checked="checked" tabindex="0" class="hidden">
				<label>可见</label>
			</div>
		</div>
		<div class="field">
			<div class="ui radio checkbox">
				<input type="radio" id="explain_view_level_0" name="explain_view_level" value="0" tabindex="0" class="hidden">
				<label>不可见</label>
			</div>
		</div>
	</div>
	<div class="field">
		<label>文字解析</label>
		<textarea rows="2" id="qexplain" name="explore" form="s_q_f_form"></textarea>
	</div>
	<div class="s_explain_appends s_appends">
		<h5 class="ui dividing header">附件列表</h5>
		<div class="ui divided items" id="explain_appends"></div>
	</div>
	<div class="ui compact menu s_append_menu">
		<a class="item s_activity_bar" onClick="chengeupdater(false,'s_q_e_q_e_div','s_q_e_q_e');">上传图片</a>
		<a class="item s_activity_bar" onClick="chengeupdater(true,'s_q_e_q_e_div','s_q_e_q_e');">上传视频</a>
	</div>
	<div class="ui attached segment" id="s_q_e_q_e" style="padding-top: 1cm; padding-bottom: 1cm;">
		<div id="s_q_e_q_e_div"></div>
	</div>
	<div class="ui error message"></div>
	<div class="field" style="padding-top: 1cm;">
		<button class="ui button" type="submit">确定</button>
	</div>
</form>
<div class="item" id="s_append_temple">
	<a class="s_type" style="display: none;"></a>
	<input type="checkbox" class="s_selector" checked="checked" style="display: none;">
	<div class="middle aligned content"></div>
	<div class="extra">
		<a class="lm_item s_rid" onClick="s_preview_append($(this))">预览</a>
		<a class="lm_item s_rid" onClick="s_remove_append($(this).attr('rid'))">删除</a>
	</div>
</div>